package com.googlecode.i78ba.forum;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

public interface PostMapper {
	@Select({"select * from post where topic_id = #{id} order by create_time desc"})
	public List<Post> findByTopicId(int id);
	
	@Select({"select * from post where topic_id = #{id} order by create_time desc limit #{rowIndex},#{pageSize}"})
	public List<Post> findByTopicIdByPage(@Param(value="id")int id,@Param(value="rowIndex")int rowIndex,@Param(value="pageSize")int pageSize);
	
	@Select({"select * from post where id = #{id}"})
	public Post findById(int id);
	
	@Insert({"insert into post (topic_id,title,create_user_id,create_time,last_update_user_id,last_update_time,reply_num,view_num)",
		"values (#{topic_id},#{title},#{create_user_id},#{create_time},#{last_update_user_id},#{last_update_time},#{reply_num},#{view_num})"})
	public void insert(Post post);
	
	@Select({"select last_insert_id()"})
	public int lastInsertId();
	
	@Select({"select count(*) from post"})
	public int findCount();
	
	@Select({"select count(*) from post where topic_id = #{id}"})
	public int findCountByTopicId(int id);
	
	@Select({"select count(*) from post where to_days(create_time) = to_days(now())"})
	public int findCountToday();
	
	@Select({"select count(*) from post where to_days(create_time) + 1 = to_days(now())"})
	public int findCountYesterday();
	
	@Update({"update post set reply_num = reply_num + 1 where id = #{id}"})
	public void addReplyNumById(int id);
	
	@Update({"update post set view_num = view_num + 1 where id = #{id}"})
	public void addViewNumById(int id);
}
